package com.hexanet.server.resources;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import com.hexanet.bom.Cell;
import com.hexanet.bom.Firewall;
import com.hexanet.bom.Location;
import com.hexanet.bom.Player;
import com.hexanet.commons.HexanetConstants;
import com.hexanet.server.dao.CellDao;
import com.hexanet.server.dao.FirewallDao;
import com.hexanet.server.dao.LocationDao;
import com.hexanet.server.dao.PlayerDao;
import com.hexanet.server.reader.HexanetReader;

import flexjson.JSONSerializer;


@Path("/cells")
public class CellResource {
    
    CellDao cellDao = new CellDao();
    PlayerDao playerDao = new PlayerDao();
    LocationDao locationDao = new LocationDao();
    FirewallDao firewallDao = new FirewallDao();
    
    @GET 
    @Produces(MediaType.APPLICATION_JSON)
    @Path("getvisiblecellsbylocation")
    public Response getVisibleCellsByLocation(@Context SecurityContext sc, InputStream inputStream) {
        
	HexanetReader<Location> reader = new HexanetReader<Location>();
	Location l = reader.getDeserializedObject(inputStream); 
	
	// use geolocation service
	List<Cell> cells = cellDao.getVisibleCellsByLocation(l);
	
	JSONSerializer serializer = new JSONSerializer();
	String json = serializer.serialize(cells);
	
	return Response.ok(json, MediaType.APPLICATION_JSON).build();
	
    }
    
    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/createhomecell")
    public Response createHomeCell(@Context SecurityContext sc, InputStream inputStream)
    {
	HexanetReader<Cell> reader = new HexanetReader<Cell>();
	Cell c = reader.getDeserializedObject(inputStream); 
	
	Player loggedin = playerDao.getByPrincipal(sc.getUserPrincipal().getName());
	c.setPlayer(loggedin);
	c.setHomebase(true);
	c.setGenerated(false);
	c.setLevel(1);
	c.setProducesEnergyBrutto(HexanetConstants.homeCellProducesEnergy);
	// on creation there exists only on firewall
	c.setProducesEnergyBrutto(HexanetConstants.homeCellProducesEnergy - HexanetConstants.levelOneFirewallEnergyConsume);
	c.setSavedEnergy(HexanetConstants.homeCellSavedEnergyStart);
	c.setSavedEnergyUpdated(new Date());
	
	if(c.getLocation() == null)
	{
	    return Response.serverError().build();
	}
	
	if(c.getName() == null || c.getName().isEmpty())
	{
	    c.setName("Homecell of " + loggedin.getNickname());
	}
	
	Firewall fw = new Firewall();
	fw.setCell(c);
	fw.setLevel(1);
	fw.setPassphrase("");
	fw.setConsumesEnergy(HexanetConstants.levelOneFirewallEnergyConsume);
	
	System.out.println("added cell = " + c.getName() + " for player " + c.getPlayer().getNickname() 
		+ " on location " + c.getLocation().getLongitude() + " - " + c.getLocation().getLatitude());
	
	locationDao.save(c.getLocation());
	cellDao.save(c);
	firewallDao.save(fw);
	
	JSONSerializer serializer = new JSONSerializer();
	String json = serializer.serialize(c);
	
	return Response.ok(json, MediaType.APPLICATION_JSON).build();
	
    } 
    
    @DELETE
    @Path("/remove/{id}")
    public Response removePlayer(@Context SecurityContext sc,
	    @PathParam("id") Long id)
    {
	
	cellDao.delete(cellDao.load(id));
	
	System.out.println("after delete");
	
	return Response.serverError().build();
	
    }
    
    @POST
    @Path("/save")
    public Response saveCell(@Context SecurityContext sc,
	    @PathParam("id") Long id)
    {
	// has to be implemented 
	return Response.serverError().build();
    }


}
